from api import ChanAPI
import utils
import matplotlib.pyplot as plt

class ThreadInfo:
    
    def __init__(self, board, number):
        api = ChanAPI(board)
        self.__data = api.getThread(number)
        self.no = number
        self.board = board

    def __process(self):
        self.posts = utils.getThreadObjects(self.__data)

    
    def calculateTimeDeltas(self):
        self.__process()
        deltas = []
        totalPosts = len(self.posts)
        
        for i, _ in enumerate(self.posts):
        
            if (i+1) < totalPosts:

                firstPostTime  = self.posts[i]["time"]
                secondPostTime = self.posts[i+1]["time"]
                delta = firstPostTime - secondPostTime

                deltas.append(abs(delta))
        return deltas

    def draw(self):
        
        plt.ylabel("seconds between post")
        plt.xlabel("post")
        
        plt.title(f"Time between posts for thread {self.no} on board {self.board}")
        deltas = self.calculateTimeDeltas()
        plt.plot(deltas)
        
        plt.savefig(f"{self.board}{self.no}.png")
        plt.show()